package com.crazyktv.wcf.android;

import android.annotation.TargetApi;
import android.app.Service;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;

import com.crazyktv.wcf.android.Util.DoControl;

@TargetApi(9)
public class MainActivity extends TabActivity {
	private TabHost tabHost;
	protected static Vibrator myVibrator = null;
	// static String appPref = "CrazyKTVPref";
	final String appPref = "com.crazyktv.wcf.android_preferences";
	final String appPrefBeginner = "settings_beginner";
	final String appPrefIP = "settings_ip";
	final String appPrefPort = "settings_port";
	final String appPrefNumLimit = "settings_num_limit";
	final String appPrefNumLen = "settings_num_len";
	final String appPrefRefreshTime = "settings_refresh_time";
	final String appPrefNewSongInnerDays = "settings_days";

	// static String appPrefIP = "PrefIP";
	// static String appPrefPort = "PrefPort";
	// static String appPrefRefreshTime = "PrefTime";
	// static String appPrefLimit = "PrefLimit";
	// static String appPrefLimitNum = "PrefLimitNum";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
			  .detectDiskReads().detectDiskWrites().detectNetwork()
			  .penaltyLog().build()); StrictMode.setVmPolicy(new
			  StrictMode.VmPolicy.Builder()
			 .detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects()
			  .penaltyLog().penaltyDeath().build());
		}

		tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab

		// Wait to play TAB
		intent = new Intent().setClass(this, WaitList.class);
		spec = tabHost
				.newTabSpec(String.valueOf(getText(R.string.tab_wait_list)))
				.setIndicator(getText(R.string.tab_wait_list),
						getResources().getDrawable(R.drawable.tab_wait_list))
				.setContent(intent);
		tabHost.addTab(spec);

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, SongQuery.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost
				.newTabSpec(String.valueOf(getText(R.string.tab_song_query)))
				.setIndicator(getText(R.string.tab_song_query),
						getResources().getDrawable(R.drawable.tab_song_query))
				.setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		// numpad TAB
		intent = new Intent().setClass(this, Numpad.class);
		spec = tabHost
				.newTabSpec(String.valueOf(getText(R.string.tab_numpad)))
				.setIndicator(getText(R.string.tab_numpad),
						getResources().getDrawable(R.drawable.tab_numpad))
				.setContent(intent);
		tabHost.addTab(spec);

		// remote control TAB
		intent = new Intent().setClass(this, Control.class);
		spec = tabHost
				.newTabSpec(String.valueOf(getText(R.string.tab_control)))
				.setIndicator(getText(R.string.tab_control),
						getResources().getDrawable(R.drawable.tab_control))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(1);
		findViews();

		// android policy
		/*
		 * StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		 * .detectDiskReads().detectDiskWrites().detectNetwork()
		 * .penaltyLog().build()); StrictMode.setVmPolicy(new
		 * StrictMode.VmPolicy.Builder()
		 * .detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects()
		 * .penaltyLog().penaltyDeath().build());
		 */

		myVibrator = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		restorePrefs();
	}

	private Button ButtonSingWith, ButtonCut;

	private void findViews() {
		ButtonSingWith = (Button) findViewById(R.id.buttonSingWith);
		ButtonCut = (Button) findViewById(R.id.buttonCut);
		ButtonSingWith.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoControl(DoControl.SINGWITH, "");
				myVibrator.vibrate(100);
			}
		});
		ButtonCut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Util.DoAction(Util.DoAction.CUT, "");
				myVibrator.vibrate(100);
			}
		});
	}

	// restore preferences
	private void restorePrefs() {
		SharedPreferences settings = getSharedPreferences(appPref, 0);
		/*
		 * String pref_ip = settings.getString(appPrefIP, "");
		 * 
		 * if (!pref_ip.equals("")) { Toast.makeText(this,
		 * getResources().getText(R.string.app_name) + " " + pref_ip,
		 * Toast.LENGTH_SHORT).show(); Util.prefIP = pref_ip; Util.prefPort =
		 * settings.getString(appPrefPort, ""); if (Util.prefPort.length() == 0)
		 * { Util.prefPort = getText(R.string.setting_default_port).toString();
		 * } Util.SERVICE_URI = "http://" + Util.prefIP + ":" + Util.prefPort +
		 * "/"; Util.prefRank = "5"; Util.prefRefresh =
		 * settings.getInt(appPrefRefreshTime, 5) * 1000 * 60; Util.prefLimit =
		 * settings.getBoolean(appPrefLimit, true); Util.prefLimitNum =
		 * settings.getInt(appPrefLimitNum, 5); } else { Intent intent = new
		 * Intent(); intent.setClass(this, Setting.class);
		 * startActivity(intent); }
		 */
		boolean pref_beginner = settings.getBoolean(appPrefBeginner, true);
		if (pref_beginner) {
			Editor editor = settings.edit();
			editor.putBoolean(appPrefBeginner, !pref_beginner);
			editor.commit();
			Intent intent = new Intent();
			intent.setClass(this, Prefs.class);
			startActivity(intent);
		} else {

			Util.prefIP = settings.getString(appPrefIP,
					(String) getText(R.string.setting_default_ip));
			Util.prefPort = settings.getString(appPrefPort,
					(String) getText(R.string.setting_default_port));
			Toast.makeText(this,
					getText(R.string.setting_ip) + "：" + Util.prefIP,
					Toast.LENGTH_SHORT).show();
			Util.SERVICE_URI = "http://" + Util.prefIP + ":" + Util.prefPort
					+ "/";
			Util.prefRank = "5";
			Util.prefNumLimit = settings.getBoolean(appPrefNumLimit, true);
			Util.prefNewSongInnerDays = Integer.parseInt(settings.getString(appPrefNewSongInnerDays, "30"));
			switch (Integer.valueOf(settings.getString(appPrefNumLen, "1"))) {
			case 0:
				Util.prefNumLen = 4;
				break;
			case 1:
				Util.prefNumLen = 5;
				break;
			case 2:
				Util.prefNumLen = 6;
				break;
			}
			int t = 0;
			switch (Integer.valueOf(settings.getString(this.appPrefRefreshTime,
					"2"))) {
			case 0:
				t = 1;
				break;
			case 1:
				t = 3;
				break;
			case 2:
				t = 5;
				break;
			case 3:
				t = 10;
				break;
			case 4:
				t = 20;
				break;
			case 5:
				t = 30;
				break;
			}

			Util.prefRefreshTime = t * 60000;

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		// menu.add(0, Menu.FIRST, 0, R.string.menu_settings);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		// case Menu.FIRST:
		case R.id.menu_settings:
			// Intent intent = new Intent();
			// intent.setClass(this, Setting.class);
			// startActivity(intent);
			startActivity(new Intent(this, Prefs.class));
			return true;
			// break;
		}
		return false;
	}
}
